package org.apache.jsp.ForumHtml;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import clientData.MemberData;
import clientData.MemberDataInt;
import clientData.ForumData;
import clientData.ForumDataInt;
import clientData.TopicData;
import clientData.TopicDataInt;
import exception.NotLoggedInException;
import java.util.Vector;
import java.util.LinkedList;
import java.rmi.Naming;
import server.ForumHandlerInt;
import java.sql.*;
import java.rmi.Naming;
import server.ForumHandlerInt;

public final class topics_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.List<java.lang.String> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.ArrayList<java.lang.String>(2);
    _jspx_dependants.add("/ForumHtml/serverConnection.jsp");
    _jspx_dependants.add("/ForumHtml/scripts.jsp");
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<java.lang.String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=utf-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
      out.write("\r\n");
      out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
      out.write("<head>\r\n");
      out.write("\r\n");
      out.write("<!-- this script is notifing the user about his friends -->\r\n");
      out.write("\r\n");
      out.write("<script type=\"text/javascript\" src=\"jquery.js\"></script>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n");
      out.write("\t\r\n");
      out.write("<title>topics </title>\r\n");
      out.write("<script src=\"SpryAssets/SpryMenuBar.js\" type=\"text/javascript\"></script>\r\n");
      out.write("<link href=\"SpryAssets/SpryMenuBarVertical.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("<link href=\"screenStyle.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("</head>\r\n");
      out.write("\t");
      out.write("\r\n");
      out.write("\r\n");

	
   String serverDomain = "localhost";
   ForumHandlerInt forum = null;
  
     try
     {
   	 forum =  (ForumHandlerInt)Naming.lookup("rmi://"+serverDomain+"/ForumService");
	   }
   catch (Exception e){
	   
	   
	   response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
	   response.flushBuffer();
		return;
   }

      out.write("\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');
      out.write('	');
 
		
		int forumId;
		if (request.getParameter("forumId")!=null)
		{
			forumId = Integer.parseInt(request.getParameter("forumId"));
			session.setAttribute("forumId",forumId);
		}
		else
			forumId = (Integer)session.getAttribute("forumId");	
		
		String forumName = request.getParameter("forumName");
			session.setAttribute("forumName",forumName);
		

		if (session.getAttribute("userName")==null)
		{
			response.sendRedirect("loggin.jsp");
			response.flushBuffer();
			return;
		}
		
		LinkedList<TopicDataInt> topics= null;
		
		try{
			topics = forum.getForumTopicList((String)session.getAttribute("userName"),(String)session.getAttribute("password"),forumId);
		}catch(NotLoggedInException t){
			response.sendRedirect("loggin.jsp");
			response.flushBuffer();
			return;	
		}
		catch(Exception e){
			response.sendError(response.SC_INTERNAL_SERVER_ERROR,"No connection to the forum server.");
			response.flushBuffer();
			return;
		}
		
	
      out.write("\r\n");
      out.write("<body>\r\n");
      out.write("\r\n");
      out.write("\t<div class=\"pageTitle\">\r\n");
      out.write("\t\t<h1>\r\n");
      out.write("\t\t\t");
 out.println(forumName+"'S TOPICS"); 
      out.write("\r\n");
      out.write("\t\t</h1>\r\n");
      out.write("\t\t<hr />\r\n");
      out.write("\t</div>\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<ul id=\"MenuBar1\" class=\"MenuBarVertical\">\r\n");
      out.write("      <li><a class=\"MenuBarItemSubmenu\" href=\"#\">manage friends</a>\r\n");
      out.write("  \t  <li><a href=\"addTopic.jsp?forumName=");
      out.print(forumName);
      out.write("&forumId=");
      out.print(forumId);
      out.write("\">add Topic</a>  </li>\r\n");
      out.write("  \t  <li><a href=\"mainScreen.jsp\">main screen</a>  </li>\r\n");
      out.write("      <li><a href=\"index.jsp\"  target=\"_top\" onClick= \"logoutFunc();\">logout</a>  </li>\r\n");
      out.write("    </ul>\r\n");
      out.write("   \r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("    var MenuBar1 = new Spry.Widget.MenuBar(\"MenuBar1\", {imgRight:\"SpryAssets/SpryMenuBarRightHover.gif\"});\r\n");
      out.write("    </script>\r\n");
      out.write("  \r\n");
      out.write("<script type=\"text/javascript\">\r\n");
      out.write("\tfunction logoutFunc() {\r\n");
      out.write("\r\n");
      out.write("\t\tvar xmlhttp;\r\n");
      out.write("\r\n");
      out.write("\t\tif (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari\r\n");
      out.write("\t\t\txmlhttp = new XMLHttpRequest();\r\n");
      out.write("\t\t} else {// code for IE6, IE5\r\n");
      out.write("\t\t\txmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n");
      out.write("\t\t}\r\n");
      out.write("\r\n");
      out.write("\t\txmlhttp.open(\"GET\", \"logout.jsp\", false);\r\n");
      out.write("\t\t//http.open(\"POST\", \"userValidation.jsp\", true);\r\n");
      out.write("\r\n");
      out.write("\t\txmlhttp.send();\r\n");
      out.write("\t\treturn xmlhttp.responseText;\r\n");
      out.write("\t}\r\n");
      out.write("</script>");
      out.write("\r\n");
      out.write(" \r\n");
      out.write("  \r\n");
      out.write("<table border=\"1\" align=\"center\">\r\n");
      out.write("\t<tbody>\r\n");
      out.write("\t\r\n");
      out.write("\t<tr>\r\n");
      out.write("\t\t<th>NAME</th>\r\n");
      out.write("\t\t<th>CREATOR</th>\r\n");
      out.write("\t\t<th>CREATION TIME</th>\r\n");
      out.write("\t</tr>\r\n");
      out.write("\t\t");

		/*ForumDataInt forumm = new ForumData(1,"cars",null);
		MemberDataInt member1 = new MemberData(111,"mari","email","marina","khokhlovkin");
		MemberDataInt member2 = new MemberData(222,"kari","email_kfj","karina","bobrik");
		MemberDataInt member3 = new MemberData(333,"ari","email_@dkmf","irena","philipchik");
		TopicDataInt topic1 = new TopicData(1,null,"title1",member1);
		TopicDataInt topic2 = new TopicData(2,null,"title2",member2);
		TopicDataInt topic3 = new TopicData(3,null,"title3",member3);
		
		LinkedList<TopicDataInt> topics = new LinkedList<TopicDataInt>();
		topics.add(topic1);
		topics.add(topic2);
		topics.add(topic3);
		out.println("<tr>");
		out.println("<td><a href=index.jsp?num=3>" + topic1.getTitle() +"</a></td>");
		out.println("<td>" + "marina" + "</td>");
		out.println("</tr>"); 
		out.println("<tr>");
		out.println("<td><a href=index.jsp>" + topic2.getTitle() +"</a></td>");
		out.println("<td>" + "marina" + "</td>");
		out.println("</tr>"); 
		out.println("<tr>");
		out.println("<td><a href=index.jsp?topicId="+topic3.getId()+">" + topic3.getTitle() +"</a></td>");
		out.println("<td>" + "marina" + "</td>");
		out.println("</tr>"); 
		out.println("<tr>");
  		out.println("<td><a href=index.jsp?topicId="+topic1.getId()+">" + topic1.getTitle() +"</a></td>");
  		out.println("<td>" + topic1.getCreator().getUserName() + "</td>");
  		out.println("</tr>"); 
		*/
		
      out.write("\r\n");
      out.write("\t\t\r\n");
      out.write("  \t");

  	
  for(TopicDataInt topic : topics){
  		out.println("<tr>");
  		out.println("<td><a href=posts.jsp?topicId="+topic.getId()+"&topicName="+topic.getTitle()+">" + topic.getTitle() +"</a></td>");
  		out.println("<td>" + topic.getCreator().getUserName() + "</td>");
  		out.println("<td>" + topic.getCreationTime()+ "</td>");
  		out.println("</tr>");
  }
  	
  	
  	
      out.write("\r\n");
      out.write("    \r\n");
      out.write("\t</tbody>\r\n");
      out.write("\r\n");
      out.write("</table>    \r\n");
      out.write("\t\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
